package cn.elwy.rcp.ui.table.provider;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableFontProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.elwy.common.util.ReflectUtil;
import cn.elwy.rcp.core.model.ui.tree.TreeElement;
import cn.elwy.rcp.ui.table.format.ColumnFormat;
import cn.elwy.rcp.ui.table.model.ColumnInfo;
import cn.elwy.rcp.ui.table.model.TableInfo;

public class TablePaneLabelProvider extends LabelProvider
    implements ITableLabelProvider, ITableColorProvider, ITableFontProvider {

  protected Logger logger = LoggerFactory.getLogger(this.getClass());

  private List<Image> needDistroyImages = new LinkedList<Image>();
  private List<Color> needDistoryColors = new LinkedList<Color>();

  private TableInfo tableInfo;

  // private IPropertyListener editorRegistryListener = new IPropertyListener() {
  // public void propertyChanged(Object source, int propId) {
  // if (propId == IEditorRegistry.PROP_CONTENTS) {
  // fireLabelProviderChanged(new LabelProviderChangedEvent(TablePaneLabelProvider.this));
  // }
  // }
  // };

  public TablePaneLabelProvider() {
    // PlatformUI.getWorkbench().getEditorRegistry().addPropertyListener(editorRegistryListener);
  }

  @Override
  public String getColumnText(Object element, int columnIndex) {
    Object text = getValue(element, columnIndex);
    return text == null ? "" : text.toString();
  }

  public Object getValue(Object element, int columnIndex) {
    Object[] texts = null;
    if (element instanceof Collection) {
      texts = (Object[]) ((Collection<?>) element).toArray();
    } else if (element instanceof Object[] || element instanceof Array) {
      texts = (Object[]) element;
    } else if (element instanceof TreeElement) {
      TreeElement item = (TreeElement) element;
      texts = item.getTexts();
      if (texts == null) {
        // texts = parseTexts(item.getItem());
        return parseText(item.getItem(), columnIndex);
      }
    }
    if (texts != null && texts.length > columnIndex) {
      return texts[columnIndex];
    }
    return parseText(element, columnIndex);
  }

  protected Object parseText(Object element, int columnIndex) {
    if (element == null) {
      return null;
    }
    ColumnInfo columnInfo = tableInfo.getColumnInfo(columnIndex);
    String columnId = columnInfo.getId();
    Object value = null;
    if (tableInfo.isTable()) {
      value = ReflectUtil.getTableColumnValue(element, columnId, true);
    } else {
      value = ReflectUtil.getTreeColumnValue(element, columnId, true);
    }
    ColumnFormat format = columnInfo.getColumnFormat();
    if (format != null && value != null) {
      value = format.format(value);
    }
    return value;
  }

  @Override
  public Image getColumnImage(Object element, int columnIndex) {
    return null;
  }

  @Override
  public Color getBackground(Object element, int columnIndex) {
    return getColor(element, columnIndex, false);
  }

  @Override
  public Color getForeground(Object element, int columnIndex) {
    return getColor(element, columnIndex, true);
  }

  private Color getColor(Object element, int columnIndex, boolean foreground) {
    return null;
  }

  @Override
  public Font getFont(Object element, int columnIndex) {
    return null;
  }

  public List<Image> getNeedDistroyImages() {
    return this.needDistroyImages;
  }

  public List<Color> getNeedDistroyColors() {
    return this.needDistoryColors;
  }

  public TableInfo getTableInfo() {
    return tableInfo;
  }

  public void setTableInfo(TableInfo tableInfo) {
    this.tableInfo = tableInfo;
  }
}
